import { ReactNode } from 'react'
import React from 'react'
import BIDemo01 from '../pages/bi/bi-demo-01'
import DynamicFormPage from '../pages/antd/dynamic'
import EchartsDemo from '../pages/echarts/demo'

/** 菜单结点信息 */
export interface AppMenuNode {
  /** menu name 可作为唯一id */
  key: string
  label?: string
  children?: AppMenu
  page?: ReactNode
}

/** 系统菜单 */
export type AppMenu = AppMenuNode[]

const BIMenu: AppMenu = [
  { key: 'drag', label: '可缩放拖拽', page: <BIDemo01 /> },
  { key: 'test2', label: '测试子菜单', children: [{ key: 'c1', label: '子1' }] }
]

const ANTD_MENU: AppMenu = [
  { key: 'dynamic', label: '动态添加表单项', page: <DynamicFormPage /> }
]

const ECHART_MENU: AppMenu = [
  { key: 'useEharts', label: 'useEhcarts', page: <EchartsDemo /> }
]

/** 全局菜单 */
export const APP_MENU: AppMenu = [
  { key: 'bi', label: 'BI📊', children: BIMenu },
  { key: 'antd', label: 'Antd', children: ANTD_MENU },
  { key: 'eharts', label: 'eharts📈', children: ECHART_MENU },
  { key: 'test', label: '测试菜单', children: [] }
]

/**
 * 根据头部菜单name获取侧边菜单
 * @param key 头部菜单key
 * @returns
 */
export const getSideMenu = (key: string): AppMenu =>
  APP_MENU.find((menu) => menu.key === key)?.children || []

const isLeafNode = (node: AppMenuNode): boolean =>
  !node?.children || node.children?.length === 0

export const getSideMenuFirstNodePath = (sideMenu: AppMenu): string[] => {
  const nodePath: string[] = []
  for (let i = 0; i < sideMenu.length; i++) {
    const curNode = sideMenu[i]
    if (isLeafNode(curNode)) {
      nodePath.push(curNode.key)
      return nodePath
    } else {
      return getSideMenuFirstNodePath(curNode.children || [])
    }
  }
  return nodePath
}
